package com.matrix.yukun.matrix.main_module.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.ColorInt;

import com.matrix.yukun.matrix.AppConstant;
import com.matrix.yukun.matrix.MyApp;
import com.matrix.yukun.matrix.main_module.entity.UserInfo;
import com.matrix.yukun.matrix.util.FileCreateUtil;
import com.matrix.yukun.matrix.util.FileUtil;

import java.io.File;
import java.util.Set;

/**
 * author: kun .
 * date:   On 2018/11/1
 */
public class SPUtils {

    private static SharedPreferences.Editor editor;
    private static SPUtils spUtils;
    private static SharedPreferences sharedPreferences;

    public static SPUtils getInstance() {
        if (spUtils == null) {
            sharedPreferences = MyApp.myApp.getSharedPreferences("share", Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
            spUtils = new SPUtils();
        }
        return spUtils;
    }

    public void saveSet(String key, Set<String> value) {
        editor.putStringSet(key, value);
        editor.apply();
    }

    public void saveString(String key, String value) {
        editor.putString(key, value);
        editor.apply();
    }

    public void saveInt(String key, int value) {
        editor.putInt(key, value);
        editor.apply();
    }

    public void saveLong(String key, long value) {
        editor.putLong(key, value);
        editor.apply();
    }
    public void saveFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.apply();
    }

    public void saveBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.apply();
    }

    public Set<String> getSet(String key) {
        return sharedPreferences.getStringSet(key, null);
    }


    public String getString(String key) {
        return sharedPreferences.getString(key, "");
    }

    public int getInt(String key) {
        return sharedPreferences.getInt(key, 0);
    }
    public float getFloat(String key) {
        return sharedPreferences.getFloat(key, 0);
    }

    public boolean getBoolean(String key) {
        return sharedPreferences.getBoolean(key, false);
    }

    public long getLong(String key) {
        return sharedPreferences.getLong(key, 0);
    }

    public void clearKey(String key) {
        if (sharedPreferences.contains(key)) {
            sharedPreferences.edit().remove(key).apply();
        }
    }

    public void setNotePassword(String password) {
        saveString("note_password", password);
    }

    public void removeNotePassword() {
        clearKey("note_password");
    }

    public String getNotePassword() {
        return getString("note_password");
    }

    public void setEditSize(int size) {
        saveInt("edit_size", size);
    }

    public int getEditSize() {
        return getInt("edit_size");
    }

    public void setEditColor(@ColorInt int color) {
        saveInt("edit_color", color);
    }

    public float getBattery() {
        return getFloat("battery");
    }

    public void setBattery(float battery) {
        saveFloat("battery", battery);
    }

    public @ColorInt
    int getEditColor() {
        return getInt("edit_color");
    }

    public void setEditBg(@ColorInt int color) {
        saveInt("edit_bg", color);
    }

    public @ColorInt
    int getEditBg() {
        return getInt("edit_bg");
    }

    public void setNoteSize(int size) {
        saveInt("note_size", size);
    }

    public int getNoteSize() {
        return getInt("note_size");
    }

    public void setNoteColor(@ColorInt int color) {
        saveInt("note_color", color);
    }

    public @ColorInt
    int getNoteColor() {
        return getInt("note_color");
    }

    public void setNoteBg(@ColorInt int color) {
        saveInt("note_bg", color);
    }

    public @ColorInt
    int getNoteBg() {
        return getInt("note_bg");
    }

    /**
     * 获取App资源路径
     *
     * @return
     */
    public static String getResourcePath() {
        String path = FileCreateUtil.getFilePath(MyApp.getContext(), AppConstant.ResourcePath);
        initFileDir(path);
        return path;
    }

    public static String getWallpaperPath() {
        return getResourcePath() + File.separator + AppConstant.WALLPAPERPATH;
    }

    public static String getImagePath() {
        return getResourcePath() + File.separator + AppConstant.IMAGEPATH;
    }

    public static String getGIFPATH() {
        return getResourcePath() + File.separator + AppConstant.GIFPATH;
    }

    public static String getGIFLOADPATH() {
        return getResourcePath() + File.separator + AppConstant.GIFLOAD;
    }

    public static String getGIFVIDEOPATH() {
        return getResourcePath() + File.separator + AppConstant.GIFVIDEO;
    }

    public static String getVIDEOPATH() {
        return getResourcePath() + File.separator + AppConstant.VIDEOPATH;
    }

    public static String getVOICEPATH() {
        return getResourcePath() + File.separator + AppConstant.VOICEPATH;
    }

    public static String getNOTEPATH() {
        return getResourcePath() + File.separator + AppConstant.NOTEPATH;
    }

    /**
     * 初始化应用文件目录
     *
     * @param appResourcePath
     */
    private static void initFileDir(String appResourcePath) {
        File appFile = new File(appResourcePath);
        if (!appFile.exists()) {
            appFile.mkdirs();
        }

        File image = new File(appFile, AppConstant.IMAGEPATH);
        if (!image.exists()) {
            image.mkdirs();
        }

        File wallpaper = new File(appFile, AppConstant.WALLPAPERPATH);
        if (!wallpaper.exists()) {
            wallpaper.mkdirs();
        }
        File GIFPATH = new File(appFile, AppConstant.GIFPATH);
        if (!GIFPATH.exists()) {
            GIFPATH.mkdirs();
        }
        File GIFLOAD = new File(appFile, AppConstant.GIFLOAD);
        if (!GIFLOAD.exists()) {
            GIFLOAD.mkdirs();
        }
        File GIFVIDEO = new File(appFile, AppConstant.GIFVIDEO);
        if (!GIFVIDEO.exists()) {
            GIFVIDEO.mkdirs();
        }
        File VIDEOPATH = new File(appFile, AppConstant.VIDEOPATH);
        if (!VIDEOPATH.exists()) {
            VIDEOPATH.mkdirs();
        }
        File VOICEPATH = new File(appFile, AppConstant.VOICEPATH);
        if (!VOICEPATH.exists()) {
            VOICEPATH.mkdirs();
        }
        File NOTEPATH = new File(appFile, AppConstant.NOTEPATH);
        if (!NOTEPATH.exists()) {
            NOTEPATH.mkdirs();
        }
    }
}
